﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System;
using System.Linq;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Collections.Generic;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.ZNH.ZMCB.UI.Utils;

namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    /// <summary>
    /// 巷道信息的新增、修改、查看页面
    /// 对应后台ServiceZMCMB05.java
    /// @author 王开兵
    /// </summary>
    /// 
    public partial class Roadway : EFViewCmes
    {
        public string roadWayNoType;//编号形式
        public string _number;//编号

        public Roadway(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            _view.Args.Add("numberType", roadWayNoType == null ? "" : roadWayNoType);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdParentId.IsEnabled = false;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, System.Windows.RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }
            if ((this.fdEndDate.Value != null) && (this.fdBeginDate.Value != null))
            {        
                if (this.fdEndDate.Value.ToString().CompareTo(this.fdBeginDate.Value.ToString()) < 0)
                {
                    this.ValidateMessagePopup("完工日期小于开工日期，请重新选择日期");//弹出提示信息
                    this.TempButtonOpen(true);//确定按钮生效
                    return;
                }
            }

            bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            //根据所属区域类型和巷道用途来设置所属工序
            CoalseamOfProcessJudge coalOfProcJudge = new CoalseamOfProcessJudge();
            this.fdProcess.Value = coalOfProcJudge.JudgeProcess(this.fdParentType.Value.ToString(), this.fdRoadwayUsing.Value.ToString());

           

            base.OnCommitButtonClick("ZMCB05", sender, e);
            //EIInfo eiInfo = this.BuildSubmitEIInfo("ZMCB05", Mode.ToString().ToLower(), EIConstants.ResultBlock);
            //base.Submit(eiInfo, DoCommitCompleted);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, System.Windows.RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 编号初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdNo_Loaded(object sender, RoutedEventArgs e)
        {
            roadWayNoType = roadWayNoType == null ? "" : roadWayNoType;

            switch (roadWayNoType)
            {
                case "0":
                    //自动
                    this.fdNo.IsReadOnly = true;
                    if (base.Mode == OperationMode.Insert)
                    {
                        this.fdNo.Value = "----------";
                    }
                    break;
                case "1":
                    //手动
                    break;
                case "2":
                    //生成
                    if (base.Mode == OperationMode.Insert)
                    {
                        this.fdNo.Value = _number;
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

         /// 剩余进尺随设计进尺而变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            fdSurplusLength.Value = fdLength.Value;
        }

        /// 所属区域类型根据所属区域而变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdParentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;

            if (field.Value == null)
            {
                return;
            }

            if (box.Items != null)
            {
                var result = from c in box.Items where c.GetType().GetProperty("value").GetValue(c, null).ToString().Equals(field.Value.ToString()) select c;

                if (result.Count() > 0)
                {
                    object Item = result.First();
                    this.fdParentType.Value = Item.GetType().GetProperty("type").GetValue(Item, null);
                }
            }
        }
    }
}
